import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)

let store=new Vuex.Store({
	state:{
		token:null,//秘钥
		schoolUrl:null,//学校的后台地址
		loginUrl:null,//登录地址
		userInfo:{},//用户信息
		userRole:null,//用户角色
		MenuList:[],//权限信息
		loginType:null,//登录来源，微信，用户名密码，
		teachDateInfo:{//教学日期信息
		    academic:'',//学期
		    semester:'',//学年
		    teachWeek:''//教学周
		    },
		todayCourse:[],
		thisCourse:{}
	},
	mutations:{
		getInfo(state,data){
			state.token=data.token
			state.schoolUrl=data.schoolUrl
			state.loginUrl=data.loginUrl
		},
		getUserInfo(state,data)
		{
			state.userInfo=data.userInfo
			state.userRole=data.userRole
			state.MenuList=data.MenuList
		},
		getTeachDateInfo(state,data){
			state.teachDateInfo=data
		},
		getCoureInfo(state,data){
			state.todayCourse=data
			state.thisCourse=data[0]
	},
	init(){
			axios.get(`${localStorage.schoolUrl}/courseDate/getDayInfo/${new Date().format("yyyy-MM-dd")}`).then(function(response){
					 console.log(response.data)
					 let myDate=new Date()
					 let week=['日','一','二','三','四','五','六']
					 let month=(myDate.getMonth()+1)+'-'+myDate.getDate()
					 let teachDateInfo= {
					 	'academic':response.data.courseAcademicYear,
					 	'semester':response.data.courseAcademicTerm,
					 	'teachWeek':response.data.courseWhichWeek,
					 	'weekDay':`周${week[myDate.getDay()]}`,
					 	'month':new Date().format("yyyy-MM-dd")
					 }
				store.commit('getTeachDateInfo',teachDateInfo)
				console.log(store.state.teachDateInfo)
					 
  			})
	      	axios.post(`${localStorage.schoolUrl}/Login/tokenLogin`,{
	      			'token':localStorage.token
	      		}).then(function(response){
	      			console.log('成功')
					if(response.data.userRole=='studentInfo'){
	      			store.commit('getUserInfo',
	      			{'userInfo':response.data.studentInfo,
	      			 'userRole':response.data.userRole,
	      			 'MenuList':response.data.MenuList
	      			})
	      		}else{
	      			store.commit('getUserInfo',
	      			{'userInfo':response.data.teacherInfo,
	      			 'userRole':response.data.userRole,
	      			 'MenuList':response.data.MenuList
	      			})
	      		}
	      axios.get(`${localStorage.schoolUrl}/course/${new Date().format("yyyy-MM-dd")}/${store.state.userInfo.studentClassId}/ClassDayCourse`).then(function (response) {
	      		store.commit('getCoureInfo',response.data)
	      		console.log(store.state.todayCourse)
	      	}).catch(function (error) {
	        	console.log(error);
	      	});

	      		}).catch(function(error){
	      			 console.log(error)
	      			 console.log('失败')
	      			// window.location.href=that.$store.state.loginUrl
	      		})
		}
	},
	actions:{
		actionGetUrl(context,info){
			context.commit('getInfo',info)
		}
	},
	dispatch:{
		getTeachDateInfo(state,data){
			state.teachDateInfo=data
		},
		}
	})

export default store
